package com.powernode.teatraceability.controller.plateprocessmgt;

import com.powernode.teatraceability.pojo.login.User;
import com.powernode.teatraceability.pojo.plateprocessmgt.ProductionEnvironment;
import com.powernode.teatraceability.service.plateprocessmgt.ProductionEnvironmentService;
import com.powernode.teatraceability.utils.GlobalConfig;
import com.powernode.teatraceability.utils.JsonData;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/production/environment")
public class ProductionEnvironmentController {

    @Resource
    private ProductionEnvironmentService productionEnvironmentService;

    @GetMapping
    public List<String> getAllCdName(@SessionAttribute User user) {
        return productionEnvironmentService.getAllCdName(user.getQydm());
    }

    @GetMapping("/{chandi}/{pageNum}/{pageSize}")
    public JsonData selectLikeByChandi(@PathVariable("pageNum") int pageNum,
                                       @PathVariable("pageSize") int pageSize,
                                       @PathVariable("chandi") String chandi,
                                       @SessionAttribute User user) {
        GlobalConfig.initPageRequest(pageNum, pageSize);
        productionEnvironmentService.selectLikeByChandinName(chandi, user.getQydm());
        return GlobalConfig.LOCAL.get();
    }

    @GetMapping("/{pageNum}/{pageSize}")
    public JsonData selectProductionEnvironmentInfo(@PathVariable int pageNum,
                                                    @PathVariable int pageSize,
                                                    @SessionAttribute User user){
        GlobalConfig.initPageRequest(pageNum,pageSize);
        productionEnvironmentService.selectAll(user.getQydm());
        return GlobalConfig.LOCAL.get();
    }

    @PostMapping
    public JsonData insertInfo(@RequestBody ProductionEnvironment productionEnvironment,
                               @SessionAttribute(name = "user", required = true) User user) {
        productionEnvironment.setQydm(user.getQydm());
        productionEnvironmentService.insert(productionEnvironment);
        return GlobalConfig.LOCAL.get();
    }

    @DeleteMapping("/{bh}")
    public JsonData deleteInfo(@PathVariable("bh") String bh) {
        productionEnvironmentService.delete(bh);
        return GlobalConfig.LOCAL.get();
    }
    @PutMapping
    public JsonData updateInfo(@RequestBody ProductionEnvironment productionEnvironment) {
        productionEnvironmentService.update(productionEnvironment);
        return GlobalConfig.LOCAL.get();
    }
}
